import 'package:flutter/material.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/my_underline_tab_indicator.dart';
import 'package:xinxuan/common/store_order_good_item.dart';
import 'package:xinxuan/controllers/store_order_controller/store_order_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/color_util.dart';

class StoreOrderPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> argumentsMap = Get.arguments;
    int tabIndex = argumentsMap['tabIndex'];
    return GetBuilder<StoreOrderPageController>(
      init: StoreOrderPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '订单列表',
            backgroundColor: pageBgColor,
          ),
          body: DefaultTabController(
            length: 5, 
            initialIndex: tabIndex,
            child: Stack(
              children: [
                Column(
                  children: [
                    Container(
                      constraints: BoxConstraints(maxHeight: 44.h),
                      child: Material(
                        color: bgWhiteColor,
                        child: TabBar(                
                          indicator: MyUnderlineTabIndicator(
                            wantWidth: 20.w,
                            borderSide: BorderSide(
                              width: 5.h,
                              color: bgMainColor
                            )
                          ),
                          indicatorColor: bgMainColor,
                          labelColor: mainTextColor,
                          unselectedLabelColor: textSubtitleColor,
                          labelStyle: TextStyle(
                            fontSize: 14.sp
                          ),
                          unselectedLabelStyle: TextStyle(
                            fontSize: 14.sp
                          ),
                          onTap: (index) {
                            // controller.tabIndex.value = index;
                            // controller.getStoreOrderList();
                            // controller.getStoreTabItemOrderList(index);

                            if (controller.tabIndex.value == index && index == 0) {
                              controller.tabIndex.value = index;
                              controller.showSelected.value = true;
                            } else {
                              controller.tabIndex.value = index;
                              controller.showSelected.value = false;
                              if (index == 0) {
                                controller.type.value = controller.typeTemp.value;
                              } else {
                                controller.type.value = '-1';
                              }
                              controller.getStoreOrderList();
                            }
                          },
                          labelPadding: EdgeInsets.zero,
                          tabs: [
                            Tab(
                              child: Obx(() => Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    controller.typeStr.value,
                                    style: TextStyle(
                                      fontSize: 14.sp
                                    )
                                  ),
                                  SizedBox(width: 4.w),
                                  Image.asset(
                                    controller.tabIndex.value == 0 ? 'assets/icons/icon_selected.png' : 'assets/icons/icon_selected_r.png',
                                    width: 8.w,
                                    height: 8.w,
                                  )
                                ],
                              )),
                            ),
                            Tab(text: '待付款'),
                            Tab(text: '待收货'),
                            Tab(text: '已完成'),
                            Tab(text: '已取消')
                          ]
                        ),
                      ),
                    ),
                    SizedBox(height: 12.h),
                    // Expanded(child: TabBarView(
                    //   physics: NeverScrollableScrollPhysics(),
                    //   children: controller.dataArr.map((item) {
                    //     return StoreOrderGoodItem(storeOrderListVo: item, storeOrderPageController: controller);
                    //   }).toList(),
                    // ))
                    Expanded(child: Obx(() => StoreOrderGoodItem(storeOrderListVo: controller.dataArr[controller.tabIndex.value], storeOrderPageController: controller)))
                  ],
                ),
                Obx(() => controller.showSelected.value ? Positioned(
                  top: 51.h,
                  left: 12.w,
                  child: Container(
                    width: 111.w,
                    height: 146.h,
                    padding: EdgeInsets.only(top: 6.h, left: 12.w, right: 12.w),
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(
                          'assets/icons/icon_selected_bg.png'
                        )
                      )
                    ),
                    child: Column(
                      children: [
                        _selectedType(title: '全部', onTap: () {
                          controller.showSelected.value = false;
                          controller.type.value = '-1';
                          controller.typeTemp.value = '-1';
                          controller.typeStr.value = '全部';
                          controller.getStoreTabItemOrderList(controller.tabIndex.value, loading: true);
                        }),
                        _selectedType(title: '普通商城', onTap: () {
                          controller.showSelected.value = false;
                          controller.type.value = '1';
                          controller.typeTemp.value = '1';
                          controller.typeStr.value = '普通商城';
                          controller.getStoreTabItemOrderList(controller.tabIndex.value, loading: true);
                        }),
                        _selectedType(title: '星选专区', onTap: () {
                          controller.showSelected.value = false;
                          controller.type.value = '3';
                          controller.typeTemp.value = '3';
                          controller.typeStr.value = '星选专区';
                          controller.getStoreTabItemOrderList(controller.tabIndex.value, loading: true);
                        }),
                      ],
                    ),
                  )
                ) : Container())
              ],
            )
          ),
        );
      },
    );
  }

  Widget _selectedType({ required String title, required Function onTap, bool showBorder = true }) {
    return InkWellUtil.shakeProofInkWell(Container(
      height: 46.h,
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        border: Border(
          bottom: showBorder ? BorderSide(
            color: Color.fromRGBO(112, 112, 112, 1),
            width: .5
          ) : BorderSide.none
        )
      ),
      child: Text(
        title,
        style: TextStyle(
          color: textWhiteColor,
          fontSize: 16.sp
        ),
      ),
    ), () {
      onTap();
    });
  }
}